package fly.gdx.game.plane;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;

import fly.game.plane.Plane;
import fly.gdx.graphics.FlyRenderer2D;
import fly.shared.Event;


public class PlaneGame implements Runnable,com.badlogic.gdx.InputProcessor
{
	public fly.game.plane.Player player = new fly.game.plane.Player(0,0,60,80);
	public ArrayList<fly.game.plane.Bullet> player_bullet = new ArrayList<fly.game.plane.Bullet>();
	public ArrayList<fly.game.plane.Enemy> enemy = new ArrayList<fly.game.plane.Enemy>();
	public ArrayList<fly.game.plane.Bullet> enemy_bullet = new ArrayList<fly.game.plane.Bullet>();
	
	private int point = 0;
	
	protected Thread game_thread;
	protected fly.gdx.graphics.FlyScene2D game_scene;
	
	private boolean game_pause = false;//< 暂停
	private boolean game_over = false;
	
	public PlaneGame()
	{
		this.game_thread = new Thread(this);
		this.game_scene = new fly.gdx.graphics.FlyScene2D(true);
		
		this.game_thread.start();
	}
	
	
	public fly.gdx.graphics.FlyScene2D GetGameScene()
	{
		return this.game_scene;
	}
	
	
	private void GameInit()
	{
		Gdx.input.setInputProcessor(this);
		
		this.game_scene.SetRenderEvent(new Event<FlyRenderer2D>() {

			@Override
			public void invoke(FlyRenderer2D renderer)
			{
				for(int i = 0;i < enemy.size();i++)
					renderer.DrawImage("assets/plane/enemy1.png",enemy.get(i).GetX(), enemy.get(i).GetY(), enemy.get(i).GetWidth(), enemy.get(i).GetHeight());
				for(int i = 0;i < player_bullet.size();i++)
					renderer.DrawImage("assets/bullet/bullet1.png",
							player_bullet.get(i).GetX(),
							player_bullet.get(i).GetY(),
							player_bullet.get(i).GetWidth(),
							player_bullet.get(i).GetHeight());
				
				renderer.DrawImage("assets/plane/player.png",player.GetX(), player.GetY(), player.GetWidth(), player.GetHeight());
				
				renderer.DrawText("分数:" + point, 10, PlaneGame.this.game_scene.GetHeight() - 10);
				renderer.DrawText("Points:" + point, 10, 20);
				
				if(game_over && !game_pause)
				{
					renderer.DrawText("GAME OVER",
							game_scene.GetWidth() / 2 - 5 * 9,
							game_scene.GetHeight() / 2 + 20);
					
					renderer.DrawText("请按空格键以重新开始游戏",
							game_scene.GetWidth() / 2 - 7 * 12,
							game_scene.GetHeight() / 2);
				}
				else if(!game_over && game_pause)
				{
					renderer.DrawText("游戏暂停中",
							game_scene.GetWidth() / 2 - 7 * 5,
							game_scene.GetHeight() / 2 + 20);
					
					renderer.DrawText("请按ESC键以继续游戏",
							game_scene.GetWidth() / 2 - 7 * 8 - 5 * 3,
							game_scene.GetHeight() / 2);
				}
			}
			
		});
		
	}
	
	
	private void DataInit()
	{
		player.SetX(this.game_scene.getWidth() / 2 - this.player.GetWidth() / 2);
		player.SetY(this.game_scene.getHeight() / 10);
		player.SetState(Plane.State.DEAD, false);
		player_bullet.clear();
		
		enemy.clear();
		enemy_bullet.clear();
		
		game_over = false;
		point = 0;
	}
	
	
	private void MainLoop()
	{
		
		int i;
		
		player.updata();
		
		/**敌机updata*/
		for(i = 0;i < enemy.size();i++)
		{
			fly.game.plane.Enemy e = enemy.get(i);
			
			e.dy -= enemy.get(i).GetSpeed();
			e.updata();
			
			boolean collision = false;
			
			for(int j = 0;j < player_bullet.size();j++)
			{
				collision = 
						player_bullet.get(j).IsCollision(e.GetX(), 
						e.GetX() + e.GetWidth(), 
						e.GetY() + e.GetHeight(), 
						e.GetY());
				
				if(collision)
				{
					e.SetState(Plane.State.DEAD, true);
					player_bullet.remove(j);
					this.point++;
				}
			}
			
			
			collision = player.IsCollision(e.GetX(), 
					e.GetX() + e.GetWidth(), 
					e.GetY() + e.GetHeight(), 
					e.GetY());
			
			if(collision)
				player.SetState(Plane.State.DEAD, true);
			
			if(enemy.get(i).GetY() + enemy.get(i).GetHeight() < 0)
				e.SetState(Plane.State.DEAD, true);
			
			
		}
		
		
		/**敌机销毁*/
		for(i = 0;i < enemy.size();i++)
		{
			if(enemy.get(i).GetState(Plane.State.DEAD))
			{
				enemy.remove(i);
			}
		}
		
		
		/**玩家子弹updata*/
		for(i = 0;i < player_bullet.size();i++)
		{
			fly.game.plane.Bullet bullet = player_bullet.get(i);
			
			if(bullet != null)
			{
				bullet.dy += player_bullet.get(i).GetSpeed();
				bullet.updata();
			}
			
			
			/**子弹销毁*/
			if(bullet != null && player_bullet.get(i).GetY() > this.game_scene.GetHeight())
			{
				player_bullet.remove(i);
			}
		}
		
		
		
		
		/**敌机刷新*/
		if(enemy.size() < 3 && this.game_scene.GetWidth() != 0)
		{
			float x = (float) (Math.random() * this.game_scene.GetWidth());
			float y = this.game_scene.GetHeight();
			
			enemy.add(new fly.game.plane.Enemy(x,y,60,80));
		}
		
		
		if(player.GetState(Plane.State.DEAD))
		{
			this.game_over = true;
			this.Fail();
		}
	}
	
	
	public void Start()
	{
		this.DataInit();
		this.game_thread.start();
	}
	
	
	public void Win()
	{
		System.out.println("哦，你胜利了！");
	}
	
	
	public void Fail()
	{
		
	}
	
	
	public void AddEnemy(fly.game.plane.Enemy e)
	{
		this.enemy.add(e);
	}
	
	
	/*
	private void KeyDown()
	{
		
		if(Gdx.input.isKeyPressed(Input.Keys.W))
			player.dy += 2;
		else if(Gdx.input.isKeyPressed(Input.Keys.S))
			player.dy -= 2;
		else if(Gdx.input.isKeyPressed(Input.Keys.A))
			player.dx -= 2;
		else if(Gdx.input.isKeyPressed(Input.Keys.D))
			player.dx += 2;
		
	}
	*/



	@Override public boolean keyDown(int keycode)
	{
		this.game_scene.keyDown(keycode);
		
		switch(keycode)
		{
		/*
		case Input.Keys.W:
			player.dy += player.GetSpeed();
			break;
		case Input.Keys.S:
			player.dy -= player.GetSpeed();
			break;
		case Input.Keys.A:
			player.dx -= player.GetSpeed();
			break;
		case Input.Keys.D:
			player.dx += player.GetSpeed();
			break;
		*/
		case Input.Keys.SPACE:
			if(!player.GetState(Plane.State.DEAD))
				player_bullet.add(
						new fly.game.plane.Bullet(
						this.player.GetX() + this.player.GetWidth() / 2 - 25,
						this.player.GetY() + this.player.GetHeight(),
						50,
						100,
						20)
					);
			else
				this.DataInit();
			
			break;
		case Input.Keys.ESCAPE:
			if(game_pause)
				game_pause = false;
			else
				game_pause = true;
			break;
		}
		
		
		return false;
	}

	@Override public boolean keyUp(int keycode)
	{
		this.game_scene.keyUp(keycode);
		
		/*
		switch(keycode)
		{
		case Input.Keys.W:
			player.dy += player.GetSpeed();
			break;
		case Input.Keys.S:
			player.dy -= player.GetSpeed();
			break;
		case Input.Keys.A:
			player.dx -= player.GetSpeed();
			break;
		case Input.Keys.D:
			player.dx += player.GetSpeed();
			break;
		}
		*/
		
		return false;
	}

	@Override public boolean keyTyped(char character)
	{
		//this.keyTyped(character);
		return false;
	}

	@Override public boolean touchDown(int screenX, int screenY, int pointer, int button)
	{
		this.game_scene.touchDown(screenX, screenY, pointer, button);
		
		/*
		if(button == Buttons.RIGHT)
			player_bullet.add(new fly.game.plane.Bullet(this.player.GetX() + this.player.GetWidth() / 2 - 25,
					this.player.GetY() + this.player.GetHeight(),50,100,20));
		*/
		
		return false;
	}

	@Override public boolean touchUp(int screenX, int screenY, int pointer, int button)
	{
		this.game_scene.touchUp(screenX, screenY, pointer, button);
		return false;
	}

	@Override public boolean touchDragged(int screenX, int screenY, int pointer)
	{
		this.game_scene.touchDragged(screenX, screenY, pointer);
		mouseMoved(screenX,screenY);
		return false;
	}

	@Override public boolean mouseMoved(int screenX, int screenY)
	{
		this.game_scene.mouseMoved(screenX, screenY);
		
		if(!game_over && !game_pause)
		{
			float draw_width_half = player.GetWidth() / 2;
			float draw_height_half = player.GetHeight() / 2;
		
			this.player.SetX(screenX - draw_width_half);
			this.player.SetY(-screenY + this.game_scene.GetHeight() - draw_height_half);
		}
		
		//System.out.println(screenX);
		//System.out.println(screenY);
		
		return false;
	}

	@Override public boolean scrolled(int amount)
	{
		this.game_scene.scrolled(amount);
		return false;
	}
	
	
	@Override public void run()
	{
		this.GameInit();
		
		
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			
			short shoot = 0;

			@Override public void run()
			{
				if(game_over || game_pause)
					return;
				
				MainLoop();
				
				/**子弹发射*/
				if(shoot == 5)
				{
					player_bullet.add(new fly.game.plane.Bullet(
							player.GetX() + player.GetWidth() / 2 - 25,
							player.GetY() + player.GetHeight(),50,100,20)
							);
					shoot = 0;
				}
				else
					shoot++;
			}
			
		};
		timer.schedule(task, 500, 1000/30);
	}
	
}



























